//
//  WDWorderOrdMainVehRemarkImageView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/2.
//

#import "WDWorderOrdMainVehRemarkImageView.h"
#import "WDGoutReviewVehicleRemarkSingleImageView.h"
#import "WDWorderOrderMaintenanceVehicleViewModel.h"
#import "EDSPublishDynamicImageVideoView.h"


@interface WDWorderOrdMainVehRemarkImageView ()

@property (nonatomic, strong) WDWorderOrderMaintenanceVehicleViewModel *viewModel;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) EDSPublishDynamicImageVideoView *imageVideoView; ///< 发布动态的视图

/// 描述
@property (nonatomic, strong) UILabel *bottomDescLabel;

@end

@implementation WDWorderOrdMainVehRemarkImageView

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor orangeColor];
    
    [self initWithTitleLabel];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(25, 2, self.width - 32, 20);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"备注图片";
            label.textColor = [UIColor colorWithHexString:@"#000000"];
        }];
        CGRect lframe = CGRectMake(20, 0, 2, 10);
        CALayer *line = [CALayer drawLine:self frame:lframe lineColor:kThemeColor];
        line.centerY = self.titleLabel.centerY;
    }
    
    // 更换配件备注图片
    if (self.viewModel) {
        // self.viewModel.reqModel.operateType
//        self.viewModel.reqModel.workResult
        if ([self.viewModel.reqModel.operateType integerValue] == 1 && [self.viewModel.reqModel.workResult isEqualToString:@"更换配件"]) {
            self.titleLabel.text = @"更换配件备注图片";
        } else {
            self.titleLabel.text = @"备注图片";
        }
    }
}

/// 更新图片
/// - Parameter model: model description
- (void)updateWithSignImage:(TYZCommonCellModel *)model {
    NSInteger index = model.index - 100;
    WDGoutReviewVehicleRemarkSingleImageView *view = [self.viewList objectOrNilAtIndex:index];
    if (view) {
        [view updateWithImage:model.image];
    }
}

/**
  动态发布的视图
 */
- (void)initWithImageVideoView
{
    if (!_imageVideoView)
    {
        UIView *preView = self.viewList.lastObject;
        float y = preView.bottom + 10;
        if (self.viewList.count == 0) {
            y = _titleLabel.bottom + 8;
        }
        
        CGRect frame = CGRectMake(20, y, self.width - 40, self.viewModel.imageHeight);
        _imageVideoView = [[EDSPublishDynamicImageVideoView alloc] initWithFrame:frame imageVideoNum:self.viewModel.addImageVideoNum imageWidth:self.viewModel.imageWidth imageHeight:self.viewModel.imageHeight];
        [self addSubview:_imageVideoView];
//        self.imageVideoView.backgroundColor = [UIColor orangeColor];
        
        // 点击视频图片
        @weakify(self);
        _imageVideoView.touchVideoImgViewblock = ^{
            @strongify(self);
            if (self.touchVideoImgViewblock)
            {
                self.touchVideoImgViewblock();
            }
        };
        // 删除图片
        _imageVideoView.deleteImageBlock = ^(NSInteger index) {
            @strongify(self);
            [self.viewModel.dataList removeObjectAtIndex:index];
            [self updateWithViewData:nil];
        };
        
        // 点击添加图片或者视频
        [[_imageVideoView.btnAddImage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.touchChoiceImageVideoBlock) {
                self.touchChoiceImageVideoBlock();
            }
        }];
    }
    
    _imageVideoView.height = self.viewModel.imagesHeight;
    
    // 图片
    self.imageVideoView.videoImgView.hidden = YES;
    NSInteger count = self.viewModel.dataList.count;
    
    for (int i=0; i<self.viewModel.addImageVideoNum; i++)
    {
        EDSPublishDynamicImageView *view = self.imageVideoView.imageList[i];
        if (i<count)
        {
            EDSImageVideoModel *model = self.viewModel.dataList[i];
            view.image = model.srcImage;
            view.hidden = NO;
            if (model.type == 1) {
                view.showPlayBtn = YES;
            } else {
                view.showPlayBtn = NO;
            }
        }
        else
        {
            view.hidden = YES;
        }
    }
    if (count >= self.viewModel.addImageVideoNum)
    {
        self.imageVideoView.btnAddImage.hidden = YES;
    }
    else
    {
        EDSPublishDynamicImageView *view = self.imageVideoView.imageList[count];
        self.imageVideoView.btnAddImage.frame = view.frame;
        self.imageVideoView.btnAddImage.hidden = NO;
    }
}

- (void)initWithBottomDescLabel {
    if (!_bottomDescLabel) {
        CGRect frame = CGRectMake(20, 0, self.width - 40, 18);
        _bottomDescLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            
        }];
    }
    if (self.viewModel) {
        NSString *str = [NSString stringWithFormat:@"最多上传%zd张", self.viewModel.addImageVideoNum];
        NSMutableAttributedString *mut = [EDSUtility attributeTitle:@"其他照片/视频" titleColor:[UIColor colorWithHexString:@"#000000"] titleFont:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] value:str valueColor:[UIColor colorWithHexString:@"#999999"] valueFont:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular]];
        self.bottomDescLabel.attributedText = mut;
        self.bottomDescLabel.top = _imageVideoView.bottom + 10;
        self.height = self.bottomDescLabel.bottom;
    }
}

- (void)updateWithViewData:(id)data {
    if (data) {
        self.viewModel = data;
    }
    [self initWithTitleLabel];
    if (self.viewList.count > 0) {
        [self initWithImageVideoView];
        
        
        [self initWithBottomDescLabel];
        self.height = self.bottomDescLabel.bottom;
        
        if (self.touchUpdateViewHeightBlock) {
            self.touchUpdateViewHeightBlock();
        }
        return;
    }
    
    
    float leftSpace = 20;
    float midSpace = 10;
    float width = (self.width - leftSpace * 2 - midSpace) * 0.5;
    float height = 100 + 8 + 20 ;
    CGRect frame = CGRectMake(leftSpace, _titleLabel.bottom + 8, width, height);
    
    WDGoutReviewVehicleRemarkSingleImageView *preView = nil;
    for (int i=0; i<self.viewModel.remarkImageInfos.count; i++) {
        TYZCommonCellModel *model = self.viewModel.remarkImageInfos[i];
        model.url = nil;
        model.index = 100 + i;
        if (i % 2 == 0) {
            if (preView) {
                frame.origin.x = leftSpace;
                frame.origin.y = preView.bottom + 10;
            } else {
                
            }
        } else {
            frame.origin.x = preView.right + midSpace;
        }
        WDGoutReviewVehicleRemarkSingleImageView *imageView = [[WDGoutReviewVehicleRemarkSingleImageView alloc] initWithFrame:frame];
        [imageView updateWithViewData:model];
        [self addSubview:imageView];
        [self.viewList addObject:imageView];
        preView = imageView;
        @weakify(self);
        // 添加图片
        imageView.viewBlock = ^(id  _Nullable data) {
            @strongify(self);
            if (self.viewBlock) {
                self.viewBlock(model);
            }
        };
        // 删除图片
        imageView.touchDelImageBlock = ^{
            @strongify(self);
            if (self.touchDelImageBlock) {
                self.touchDelImageBlock(model);
            }
        };
    }
    
    [self initWithImageVideoView];
    
    [self initWithBottomDescLabel];
    
//    self.height = _imageVideoView.bottom;
    self.height = self.bottomDescLabel.bottom;
    if (self.touchUpdateViewHeightBlock) {
        self.touchUpdateViewHeightBlock();
    }
}


@end
